// JavaScript Document

 function getBasePath(){ 
	 var obj=window.location; 
	 var contextPath=obj.pathname.split("/")[1]; 
	 var basePath=obj.protocol+"//"+obj.host+"/"+contextPath; 
	 return basePath; 
} 

window.onload=function(){
 	var warp=document.getElementById("warp");
	var oUl=warp.getElementsByTagName("ul")[0];
	var aImg=oUl.getElementsByTagName("img");
	oUl.innerHTML=oUl.innerHTML+oUl.innerHTML;
	oUl.style.width=aImg[0].offsetWidth*aImg.length+'px';
	var timer=null;
	function TurnLeft(){
		if(oUl.offsetLeft>(-oUl.offsetWidth/2))
		{
			oUl.style.left=oUl.offsetLeft-2+'px';
		}
		else
		{
			oUl.style.left="0px";
		}
	}
	timer=setInterval(TurnLeft,30);
	oUl.onmouseover=function(){
		clearInterval(timer);
	}
	oUl.onmouseout=function(){
		timer=setInterval(TurnLeft,30);
	}
	
	//瀑布流
	waterFull("main","box");
}



function getIndexByminH(harr,minH)
{
		for(var i=0;i<harr.length;i++)
		{
			if(harr[i]==minH)
			{
				return i;
			}
		}

}	
function waterFull(parent,clsName){
	var Oparent=document.getElementById("main");
	var aBox=Oparent.getElementsByClassName("box");
	var oBoxW=aBox[0].offsetWidth;
	var clientW=document.documentElement.clientWidth;
	//console.log(clientW);
	//最多可以放几列图片
	var cols=Math.floor(clientW/oBoxW);
	Oparent.style.cssText="width:"+oBoxW*cols+"px;margin:0 auto;"
	//存储每一列的高度
	var harr=[];
	for(var i=0;i<aBox.length;i++)
	{
		if(i<cols){
			harr.push(aBox[i].offsetHeight);
			console.log(aBox[i].offsetHeight+"#");
		}else{
			var minH=Math.min.apply(null,harr);
			//获取当前行中最小的图片高度
			var index=getIndexByminH(harr,minH);
			aBox[i].style.position="absolute";	
			aBox[i].style.top=minH+"px";
			aBox[i].style.left=aBox[index].offsetLeft+"px";
			harr[index]=harr[index]+aBox[i].offsetHeight;
		}
	}
	
	//采用面向对象工厂方法设计模式动态创建选项卡
	function Factory(type,content){
                if(this instanceof Factory)
                {
                    var s=this[type](content);
                }
                else
                {
                    return new Factory(type,content);
                }
            }
            Factory.prototype= {
                theme: function (content) {
                    this.content = content;
                    (function (content) {
                                var box = document.getElementById("menu");
                                var oUl = box.getElementsByTagName("ul")[0];
                                var oli = document.createElement("li");
                                var oA = document.createElement("a");
                                oA.href= getBasePath() + "/diary/home.action";
                                oA.innerHTML=content;
                                oli.appendChild(oA);
                                oUl.appendChild(oli);
                    })(content)
                },
                journey: function (content) {
                    this.content = content;
                    (function (content) {
                        var box = document.getElementById("menu");
                        var oUl = box.getElementsByTagName("ul")[0];
                        var oli = document.createElement("li");
                        var oA = document.createElement("a");
                        oA.href=getBasePath() + "/diary/list.action";
                        oA.innerHTML=content;
                        oli.appendChild(oA);
                        oUl.appendChild(oli);
                    })(content)
                },
                photo: function (content) {
                    this.content = content;
                    (function (content) {
                        var box = document.getElementById("menu");
                        var oUl = box.getElementsByTagName("ul")[0];
                        var oli = document.createElement("li");
                        var oA = document.createElement("a");
                        oA.href= getBasePath() + "/diary/photo.action";
                        oA.innerHTML=content;
                        oli.appendChild(oA);
                        oUl.appendChild(oli);
                    })(content)
                },
                personnel: function (content) {
                    this.content = content;
                    (function (content) {
                        var box = document.getElementById("menu");
                        var oUl = box.getElementsByTagName("ul")[0];
                        var oli = document.createElement("li");
                        var oA = document.createElement("a");
                        oA.href= getBasePath() + "/diary/personnel.action";
                        oA.innerHTML=content;
                        oli.appendChild(oA);
                        oUl.appendChild(oli);
                    })(content)
                }
            };
            var data=[{type:"theme",content:"首页"},
                    {type:"journey",content:"日志"},
                    {type:"photo",content:"相册"},
                    {type:"personnel",content:"个人"}
            ];
            for(var i=0;i<data.length;i++)
            {
                Factory(data[i].type,data[i].content);
            }
			
}